package com.qf.service.serviceImpl;

import com.qf.dao.PlayMapper;
import com.qf.pojo.*;
import com.qf.service.PlayService;
import com.qf.utils.JwtUtils;
import com.qf.utils.MyJia;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlayServiceImpl implements PlayService {

    @Autowired
    PlayMapper playMapper;

    @Override
    public BaseResp del(Integer id) {
        BaseResp baseResp = new BaseResp();
        if (id!=null){
            playMapper.del(id);
            baseResp.setMgs("删除成功");
            baseResp.setCode(200);
            return baseResp;
        }
        baseResp.setCode(203);
        baseResp.setMgs("删除失败");
        return baseResp;
    }

    @Override
    public BaseResp update(Play play) {
        BaseResp baseResp = new BaseResp();
        int update = playMapper.update(play);
        if (update==1){
            baseResp.setMgs("修改成功");
            baseResp.setCode(200);
            return baseResp;
        }
        baseResp.setCode(203);
        baseResp.setMgs("修改失败");
        return baseResp;
    }

    @Override
    public BaseResp findOne(String playname) {
        BaseResp baseResp = new BaseResp();
        Play pass = playMapper.findPass(playname);
        baseResp.setData(pass);


        return baseResp;
    }

    @Override
    public BaseResp findAll(String gname , int page ,int size) {
        BaseResp baseResp = new BaseResp();
        int o = page-1;

        List<Play> all = playMapper.findAll(gname, o*size,size);
        int count = playMapper.findCount(gname);
        baseResp.setTotal(count);
        baseResp.setData(all);
        baseResp.setMgs("查询成功");
        baseResp.setCode(200);
        return baseResp;
    }

    @Override
    public BaseResp updateprice(PlayGame playGame) {
        BaseResp baseResp = new BaseResp();
        Game gnameBygid = playMapper.findgid(playGame.getGname());
        playGame.setGid(gnameBygid.getId());
        int updateprice = playMapper.updateprice(playGame);
        if (updateprice==1){
            baseResp.setCode(200);
            baseResp.setMgs("修改成功");
            return baseResp;
        }else {
            baseResp.setCode(2050);
            baseResp.setMgs("修改失败");
            return baseResp;
        }
    }

    @Override
    public BaseResp findGame() {
        BaseResp baseResp = new BaseResp();
        List<Game>  game = playMapper.findGame();
        baseResp.setData(game);

        return baseResp;
    }

    @Override
    public BaseResp upo(Orders orders) {
        BaseResp baseResp = new BaseResp();
        int upo = playMapper.upo(orders);
        if (upo==1){
            baseResp.setMgs("修改成功");
            baseResp.setCode(200);
            return baseResp;
        }else {
            baseResp.setMgs("修改失败");
            baseResp.setCode(20011);
            return baseResp;
        }
    }

    @Override
    public BaseResp findplaygame(Integer pid) {
        BaseResp baseResp = new BaseResp();
        List<PlayGame> findplaygame = playMapper.findplaygame(pid);
        if (findplaygame==null){
            baseResp.setMgs("还没有会玩的游戏");
            return baseResp;
        }else {
            baseResp.setData(findplaygame);
            baseResp.setCode(200);
            baseResp.setMgs("查询成功 ");
            return baseResp;
        }

    }

    @Override
    public BaseResp login(Play play) {
        BaseResp baseResp = new BaseResp();
        Play pass = playMapper.findPass(play.getName());
        String jiami = MyJia.jiami(play.getPassword());

        play.setPassword(jiami);

        if(StringUtils.isEmpty(play.getName())){
            baseResp.setCode(201);
            baseResp.setMgs("用户名或邮箱为空");
            return baseResp;
        }

        if(pass == null){
            baseResp.setCode(202);
            baseResp.setMgs("用户名或邮箱不存在");
            return baseResp;
        }

        if(StringUtils.isEmpty(play.getPassword())){
            baseResp.setCode(203);
            baseResp.setMgs("密码为空");
            return baseResp;
        }

        if(!jiami.equals(pass.getPassword())){
            baseResp.setCode(204);
            baseResp.setMgs("密码错误");
            return baseResp;
        }

        JwtUtils jwtUtils = new JwtUtils();
        Map map = new HashMap();
        String name = "playName";
        map.put(name, play.getName());
        String token = jwtUtils.createToken(map,name);

        baseResp.setData(token);
        baseResp.setCode(200);
        baseResp.setMgs("登录成功");
        return baseResp;
    }


}
